package com.kkd.kkdapp.adapter;

import java.text.DecimalFormat;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.http.entity.MyExperienceGoldItem;
import com.kkd.kkdapp.listener.OnSingleClickListener;

public class MyExperienceAdapter extends BaseAdapter {
	private Context mContext;
	private LayoutInflater inflater;
	private List<MyExperienceGoldItem> list;
	private DecimalFormat df = new DecimalFormat("#,###.00");

	public MyExperienceAdapter(Context context, List<MyExperienceGoldItem> list) {
		mContext = context;
		inflater = LayoutInflater.from(mContext);
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_myexperiencegold,
					parent, false);
			// 标题
			holder.title_item_myExperienceGold = (TextView) convertView
					.findViewById(R.id.title_item_myExperienceGold);
			// 类型
			holder.bidType_item_myExperienceGold = (TextView) convertView
					.findViewById(R.id.bidType_item_myExperienceGold);
			// 年化收益
			holder.rate_item_myExperienceGold = (TextView) convertView
					.findViewById(R.id.rate_item_myExperienceGold);
			// 期限
			holder.limit_item_myExperienceGold = (TextView) convertView
					.findViewById(R.id.limit_item_myExperienceGold);
			// 可购买金额
			holder.numCanBuy_item_myExperienceGold = (TextView) convertView
					.findViewById(R.id.numCanBuy_item_myExperienceGold);
			// 百分比
			holder.percent_item_myExperienceGold = (TextView) convertView
					.findViewById(R.id.percent_item_myExperienceGold);
			// 进度条
			holder.progressBar_item_myExperienceGold = (ProgressBar) convertView
					.findViewById(R.id.progressBar_item_myExperienceGold);
			// 认购
			holder.tender_item_myExperienceGold = (TextView) convertView
					.findViewById(R.id.tender_item_myExperienceGold);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		/*
		 * 设置标题
		 */
		holder.title_item_myExperienceGold.setText(list.get(position)
				.getBidTitle());
		/*
		 * 设置类型
		 */
		holder.bidType_item_myExperienceGold.setText(list.get(position)
				.getBidTypeName());
		holder.bidType_item_myExperienceGold.setTextColor(Color.parseColor(list
				.get(position).getBidTypeColor()));
		/*
		 * 年化收益
		 */
		holder.rate_item_myExperienceGold.setText(list.get(position)
				.getInterestDisplay() + "");
		/*
		 * 投资期限
		 */
		holder.limit_item_myExperienceGold.setText(list.get(position)
				.getBidPeriodValue());
		/*
		 * 可购买金额
		 */
		holder.numCanBuy_item_myExperienceGold.setText(df.format(list.get(
				position).getBidFreeAmount() / 100f));
		/*
		 * 进度
		 */
		int bidAmout = (int) (list.get(position).getBidAmount() / 100);
		double bidFreeAmount = list.get(position).getBidFreeAmount() * 0.01;
		int interest = (int) Math.ceil((1 - bidFreeAmount / bidAmout) * 100);
		holder.progressBar_item_myExperienceGold.setProgress(interest);
		holder.percent_item_myExperienceGold.setText(list.get(position)
				.getBidProgress() + "%");
		/*
		 * 认购
		 */
		holder.tender_item_myExperienceGold
				.setOnClickListener(new OnSingleClickListener() {

					@Override
					public void onSingleClick(View v) {

					}
				});
		return convertView;
	}

	class ViewHolder {
		TextView title_item_myExperienceGold;
		TextView bidType_item_myExperienceGold;
		TextView rate_item_myExperienceGold;
		TextView limit_item_myExperienceGold;
		TextView numCanBuy_item_myExperienceGold;
		TextView percent_item_myExperienceGold;
		TextView tender_item_myExperienceGold;
		ProgressBar progressBar_item_myExperienceGold;
	}
}
